package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.UpdateUserRoleDto;
import com.group.eams.pojo.dto.UserAddDto;
import com.group.eams.pojo.dto.UserLoginDto;
import com.group.eams.pojo.dto.UserQueryDto;
import com.group.eams.pojo.entity.User;
import com.group.eams.pojo.vo.LoginInfoListVo;
import com.group.eams.pojo.vo.UserListVo;
import com.group.eams.service.UserService;
import com.group.eams.threadlocal.UserThreadLocal;
import com.group.eams.threadlocal.UserThreadLocalDto;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户管理(User)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/user")
public class UserController  {

    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public Result logout(){
        UserThreadLocal.remove();
        return Result.buildSuccess("退出成功");
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody UserLoginDto userLoginDto){
        //1.调用service进行处理
        String token = userService.login(userLoginDto);
        //2.返回数据给前端
        return  Result.buildSuccess(token);
    }

    //获取登录信息
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public Result getInfo(){


        //新建loginInfoListVo，用来返回头像和名字
        LoginInfoListVo loginInfoListVo=new LoginInfoListVo();

        UserThreadLocalDto userThreadLocalDto = UserThreadLocal.get();

        //从线程里获取名字和头像
        String name = userThreadLocalDto.getName();
        String image = userThreadLocalDto.getImage();
        loginInfoListVo.setName(name);
        loginInfoListVo.setAvatar(image);

        return Result.buildSuccess(loginInfoListVo);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody UserAddDto userAddDto){
        //1.校验

        //2.拷贝数据
        User user=new User();
        BeanUtils.copyProperties(userAddDto,user);

        //3.调用service进行处理
        userService.insertUser(user);

        //返回结果
        return  Result.buildSuccess();
    }

    @RequestMapping(value = "/updateUserRole",method = RequestMethod.POST)
    @ApiOperation("修改用户的角色")
    public Result updateUserRole(@RequestBody UpdateUserRoleDto updateUserRoleDto){
        userService.updateUserRole(updateUserRoleDto);
        return Result.buildSuccess();
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "用户列表",httpMethod = "GET")
    public Result list(@RequestBody UserQueryDto userQueryDto){

        //1.准备分页条件
        Page<UserListVo> page=new Page<>(userQueryDto.getPage(),userQueryDto.getLimit());


        //3.查询分页数据
        Page<UserListVo> userByQueryDto = userService.findUserByQueryDto(page, userQueryDto);

        //返回结果
        return Result.buildSuccess(userByQueryDto.getTotal(),userByQueryDto.getRecords());
    }

    @RequestMapping(value = "/find",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找用户")
    public Result find(@RequestParam Long id){
        User user = userService.getById(id);
        return Result.buildSuccess(user);
    }
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value = "查找所有用户")
    public Result findAll(){
        List<User> userList = userService.list();
        return Result.buildSuccess(userList);
    }


    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation("修改用户")
    public Result update(@RequestBody User user){
        boolean result = userService.updateById(user);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ApiOperation("根据id删除用户")
    public Result delete(@RequestParam Long id){
        boolean result = userService.removeById(id);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }


    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    @ApiOperation("批量删除用户")
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){
        boolean result = userService.removeByIds(batchDeleteDto.getIds());
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }
}

